package com.example.lotterysystem.common.pojo;

import com.example.lotterysystem.common.errorcode.ErrorCode;
import com.example.lotterysystem.common.errorcode.GlobalErrorCodeConstants;
import com.example.lotterysystem.common.exception.GlobalExecption;
import lombok.Data;
import org.springframework.util.Assert;

import java.io.Serializable;
@Data
public class CommonResult<T> implements Serializable {
    /**
     * 状态码
     */
    private Integer code;
    /**
     * 返回数据
     */
    private T data;
    /**
     * 报错信息
     */
    private String msg;
    public static <T> CommonResult<T> success(T data) {
        CommonResult<T> result = new CommonResult<>();
        result.code = GlobalErrorCodeConstants.SUCCESS.getCode();
        result.data = data;
        result.msg = "";
        return result;
    }

    public static <T> CommonResult<T> error(Integer code, String msg) {
        //断言一下，如果传过来的是200，说明传错了
        Assert.isTrue(!GlobalErrorCodeConstants.SUCCESS.getCode().equals(code), "code,不是错误码");
        CommonResult<T> result = new CommonResult<>();
        result.code = code;
        result.msg = msg;
        return result;
    }
    public static <T> CommonResult<T> error(ErrorCode errorCode) {
        return error(errorCode.getCode(), errorCode.getMsg());
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
